import UIUtils from "../base/UiUtils";
import View from "../base/View";
import { EPokerStatus, Esuit } from "../ConfigEnum";
import SplashScene from "../SplashScene";
import UIPoker from "../UIPoker";
import GameDB, { Poker } from "./GameDB";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameScene extends cc.Component{


    @property(cc.Prefab)    pokerPrefab: cc.Prefab = null;
    @property(cc.Node)    initPokerArea: cc.Node = null;

    @property(cc.Node)    closeSendArea: cc.Node = null;
    @property(cc.Node)    openSendArea: cc.Node = null;
    @property([cc.Node])    receiveAreaList: cc.Node[] = [];
    @property(cc.Node)    playGroupRoot: cc.Node = null;
    // private playGroupList: cc.Node[] = [];

    public onLoad(): void {
        // for(let i = 0;i<GameDB.CONST_PLAY_GROUPS;i++){
        //     let playGroup = new cc.Node()
        //     playGroup.position = new cc.Vec3(85*i, 0, 0)
        //     this.playGroupRoot.addChild(playGroup)
        //     this.playGroupList.push(playGroup)
        // }

        //所有节点都放在同一个root下面

    }

    private OnPlay () {
        let stack :cc.Node[] = [];
        for(let i = this.initPokerArea.children.length-1;i>=0;i--){
            let child =this.initPokerArea.children[i];
            stack.push(child);
            this.initPokerArea.removeChild(child);
        }
        for (let i = stack.length -1;i>=0; --i){
            let child = stack[i]
            this.closeSendArea.addChild(child);
        }
    } 

    public OnEventInit (pokers){
        this.InitPokers(pokers);
    }
    public OnEventPlay () {
        console.log("OnPlay");

        this.OnPlay();
    }
    // private OnEventClickPoker (poker: Poker) {
    //     SplashScene.eventTarget.emit(GameEvent.CLICK_POKER,poker)
    //     console.log("OnClickPoker",poker)
    // }
    public InitPokers(pokers: Poker[]){
        //展示所有扑克ui
        console.log("InitPokers");
        pokers.forEach((poker,index)=>{
            let uiPoker = this.CreateUIPoker(poker);
            uiPoker.node.x = 0.2*index
            uiPoker.node.y = 0.2*index
            this.initPokerArea.addChild(uiPoker.node);
        })    
    }
    private  CreateUIPoker(poker:Poker): UIPoker {
        let uiPokerNode =cc.instantiate(this.pokerPrefab);
        let uiPoker = uiPokerNode.getComponent(UIPoker);
        uiPoker.Init(poker);
        // SplashScene.eventTarget.on(GameEvent.CLICK_POKER,this.OnEventClickPoker,this);
        return uiPoker;
    }

    public OnEventINitGroupCard(groupIndex: number,cardIndex: number,poker: Poker) {
       let index = GameDB.CONST_PLAY_GROUPS*cardIndex - cardIndex*(cardIndex+1)/2 - cardIndex+groupIndex;
    //    console.log(`g:${groupIndex} c:${cardIndex} p:${poker.point} index:${index}`)
       //先移动ui
       let node = poker.view.node

       UIUtils.move(node,this.playGroupRoot)
       node.zIndex = index
            
            
    //    let wp = node.convertToWorldSpaceAR(cc.v3(0,0,0))
    //    let gp = this.playGroupRoot.convertToNodeSpaceAR(wp)
    //    node.removeFromParent()
    //    node.position = gp;
    //    this.playGroupRoot.addChild(node,index)
    //    node.x = gp.x
    //    node.y = gp.y
    //    node.setSiblingIndex(index)

       let px = groupIndex*85;

       let delay = index * 0.1
       if(poker.status == EPokerStatus.OPEN){
        cc.tween(node)
        .delay(delay)
        .to(0.5 , {position: cc.v3(px,-30*cardIndex)})
        .to(0.3, {scaleX: 0})
        .call(()=>{
            //ui的显示状态更新
            poker.view.Rrfresh()
        })
        .to(0.3, {scaleX: 1})
        .start()
     }else{
        cc.tween(node)
        .delay(delay)
        .to(0.5 , {position: cc.v3(px,-30*cardIndex)})
        .start()
     }

    }

}